package com.epfresh.supplier.activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ListView;
import android.widget.TextView;

import com.epfresh.api.adapter.CommonAdapter;
import com.epfresh.api.adapter.ViewHolder;
import com.epfresh.api.constant.Constant;
import com.epfresh.api.entity.RequestEntitySupply;
import com.epfresh.api.entity.ResponseEntity;
import com.epfresh.api.global.BaseActivity;
import com.epfresh.api.global.BaseApplication;
import com.epfresh.api.http.OnRequestListener;
import com.epfresh.supplier.R;
import com.google.gson.Gson;
import com.epfresh.supplier.adapter.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.epfresh.supplier.entity.*;
public class BookDetailDayActivity extends BaseActivity {


    private TextView tv_time_now,txt_deal_money,txt_service_fee,txt_should_money,txt_already_money;
    View tvMoreBest,tvMoreDetail,ivTime;
    ListView lvDetail;

    String timeValue;

    ArrayList<DealDetailEntity.ContentBean> detailList = new ArrayList<>();
    DetailAdapter detailAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_book_day_detail);
        timeValue = getIntent().getStringExtra("timeValue");
        toolbarTitle.setText(/*timeTitle+*/"交易明细");
        ivBack.setVisibility(View.VISIBLE);

        View headView;
        headView = getLayoutInflater().inflate(R.layout.include_book_head_detail_day, null);

        tv_time_now = (TextView) headView.findViewById(R.id.tv_time_now);
        txt_deal_money = (TextView) headView.findViewById(R.id.txt_deal_money);
        txt_service_fee = (TextView) headView.findViewById(R.id.txt_service_fee);
        txt_should_money = (TextView) headView.findViewById(R.id.txt_should_money);
        txt_already_money = (TextView) headView.findViewById(R.id.txt_already_money);
        lvDetail = findViewById(R.id.lv_deal_detail);
//        lvDetail.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
//            @Override
//            public boolean onGroupClick(ExpandableListView parent, View v,
//                                        int groupPosition, long id) {
//                return true;
//            }
//        });
        lvDetail.addHeaderView(headView);
        detailAdapter = new DetailAdapter(this,detailList,R.layout.item_deal_day_detail_new);
        lvDetail.setAdapter(detailAdapter);

//        tv_time_now.setText(timeTitle);


        mHttpLoad();
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }

    /**
     * 加载收入数据
     */
    private void mHttpLoad() {
        RequestEntitySupply<Map<String, Object>> data = new RequestEntitySupply<>();
        data.setCmd(com.epfresh.api.constant.AppSupplyRouterConstant.cmd_finance_settle_income_orderTable);
        Map<String, Object> map = new HashMap<>();
        map.put("storeId", BaseApplication.getInstance().getStoreId());

        map.put("queryDate", timeValue);
        map.put("authNeedBind",BaseApplication.getInstance().getMerchant().isAuthNeedBind());
        data.setParameters(map);
        request(data, com.epfresh.api.constant.AppSupplyRouterConstant.cmd_finance_settle_income_orderTable, onRequestListener);
    }

    private OnRequestListener<DealDetailEntity> onRequestListener = new OnRequestListener<DealDetailEntity>() {
        @Override
        public DealDetailEntity jsonToObj(String responseStr) {
            return new Gson().fromJson(responseStr, DealDetailEntity.class);
        }

        @Override
        public void onFail(int failCode, Object arg, Object tag) {
            hideProgressDialog();
        }

        @Override
        public void onResponseError(int failCode, Object arg, Object tag) {
            hideProgressDialog();
        }

        @Override
        public void onStart(Object tag) {
            showProgressDialog();
        }

        @Override
        public void onResponse(ResponseEntity<DealDetailEntity> response, Object tag) {

            hideProgressDialog();
            DealDetailEntity responseElement = response.getResponseElement();

            tv_time_now.setText(responseElement.getQueryDateStr());
            txt_deal_money.setText("交易金额:" + responseElement.getTotalOrderAmount() + "元");
            txt_service_fee.setText("服务费:" + responseElement.getTotalServiceFee() + "元");
            txt_should_money.setText("应结金额:" + responseElement.getTotalReceivableAmount() + "元");
            txt_already_money.setText("已结金额:" + responseElement.getTotalSettledAmount() + "元");

            detailList.addAll(responseElement.getContent());
            detailAdapter.notifyDataSetChanged();

        }
    };

    class DetailAdapter extends CommonAdapter<DealDetailEntity.ContentBean> implements View.OnClickListener{

        public DetailAdapter(Context context, List<DealDetailEntity.ContentBean> datas, int layoutId) {
            super(context, datas, layoutId);
        }

        @Override
        public int getCount() {
            if (mDatas == null) {
                return 0;
            }
            return mDatas.size();
        }

        @Override
        public void convert(ViewHolder holder, DealDetailEntity.ContentBean contentBean) {
            holder.getView(R.id.ll_item).setTag(R.id.item_key_position,contentBean);
            holder.setOnClickListener(R.id.ll_item,this);

            if(holder.getPosition()%2==1){
                holder.getView(R.id.ll_item).setBackgroundColor(Color.WHITE);
            }else {
                holder.getView(R.id.ll_item).setBackgroundColor(Color.parseColor("#fcfcfc"));
            }

            holder.setText(R.id.txt_order_no,contentBean.getOrderNo());
            if (contentBean.getStatus().equals("0")) {
                holder.setText(R.id.txt_settlement_status,"待结算");
                holder.setTextColor(R.id.txt_settlement_status,Color.parseColor("#ff0000"));
            } else if (contentBean.getStatus().equals("6")) {
                holder.setText(R.id.txt_settlement_status,"已结算");
                holder.setTextColor(R.id.txt_settlement_status,Color.parseColor("#434343"));
            } else {
                holder.setText(R.id.txt_settlement_status,contentBean.getStatus());
                holder.setTextColor(R.id.txt_settlement_status,Color.parseColor("#434343"));
            }
            holder.setText(R.id.txt_deal_money,contentBean.getOrderAmount());
            holder.setText(R.id.txt_service_fee,contentBean.getServiceAmount());
            holder.setText(R.id.tv_money_should,contentBean.getReceivableAmount());


        }

        @Override
        public void onClick(View v) {
            DealDetailEntity.ContentBean bean = null;
            Object tag = v.getTag(R.id.item_key_position);
            if (tag != null && tag instanceof DealDetailEntity.ContentBean) {
                bean = (DealDetailEntity.ContentBean) tag;
            }
            if (bean == null) {
                return;
            }
            switch (v.getId()) {
                case R.id.ll_item:
                    Intent detail = new Intent(BookDetailDayActivity.this,H5OrderDetailActivity.class);
                    detail.putExtra("orderId", bean.getOrderId());
                    detail.putExtra(Constant.KEY_WEB_URL, "http://m.epfresh.com/help/order_supply_new.html?orderId="+bean.getOrderId());
                    detail.putExtra(Constant.KEY_WEB_TITLE, "订单详情");
                    startActivity(detail);
                    break;
            }
        }
    }
}
